/***************************************************************************
 *   oboe.controllers.PhysicsController                                    *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.controllers
{
	import flash.display.DisplayObject;
	import oboe.core.SigSlot;
	import oboe.functors.ApplyGravitationalForces;
	
	public class PhysicsController extends SigSlot
	{
		private static const MAX_SPEED:Number = 100;
		
		private var asset:DisplayObject;
		private var friction:Number;
		private var rotateToTarget:Boolean;
		private var velocityX:Number;
		private var velocityY:Number;
		private var positionX:Number;
		private var positionY:Number;
		
		private var forceAcc:ApplyGravitationalForces;
		
		
		public function PhysicsController( asset:DisplayObject, rotateToTarget:Boolean=false, posX:Number=0, posY:Number=0, friction:Number=0 )
		{
			this.asset = asset;
			this.velocityX = 0;
			this.velocityY = 0;
			this.positionX = posX;
			this.positionY = posY;
			this.friction = friction;
			this.rotateToTarget = rotateToTarget;
			this.forceAcc = new ApplyGravitationalForces();
		}
		
		public function slot_onAddForceVector( x:Number, y:Number ):void
		{
			this.velocityX += x;
			this.velocityY += y;
		}
		
		public function slot_onResetForces():void
		{
			this.velocityX = this.velocityY = 0;
		}
		
		public function slot_onReset(posX:Number, posY:Number, friction:Number):void
		{
			this.velocityX = this.velocityY = 0;
			this.positionX = posX;
			this.positionY = posY;
			this.friction = friction;
		}

		public function slot_onTick( time:Number ):void
		{
			this.forceAcc.reset();
			this.signal("onApplyGravitationalForces", this.forceAcc, this.positionX, this.positionY );
			
			time=0.1;
			
			if (this.forceAcc.collided)
			{
				this.shortSignal("onCollision");
				return;
			}
			
			if (Math.abs(this.velocityX) < MAX_SPEED && Math.abs(this.velocityY) < MAX_SPEED)
			{
				this.velocityX += this.forceAcc.getX() * time;
				this.velocityY += this.forceAcc.getY() * time;
			}
			
			var dx:Number = this.velocityX;
			var dy:Number = this.velocityY;
			
			this.positionY += this.velocityY * time;				
			this.positionX += this.velocityX * time;
			
			var mag:Number = 1/(1+friction*time);
			this.velocityX *= mag;
			this.velocityY *= mag;
			if( this.rotateToTarget && (Math.abs(dx)+Math.abs(dy))>0.1 )
			{
				this.asset.rotationZ = Math.atan2( dx, -dy )/Math.PI*180;
			}
			this.shortSignal("onUpdatePhysics", this.positionX, this.positionY );
		}
		
		public function slot_onUpdateCameraPosition( x:Number, y:Number ):void
		{
			this.asset.x = this.positionX-x;
			this.asset.y = this.positionY-y;
		}
	}
}